#ifndef _SERVER_H_
#define _SERVER_H_

#include "types.h"
#include "mutex.h"
#include "thread.h"

namespace reldt {

class Server
{
public:
	Server();
	~Server();

    int accept(const char *src_ip, u16 src_port, int timeout); //block

    int disconn();
	virtual void on_disconn() {}

    int send(const char *msg, int msglen);
    virtual void on_finish() {}

	virtual void on_recv(const char *msg, int msglen) {}

private:
    enum
    {
        STATE_IDLE,
        STATE_ACCEPTING,
        STATE_CONNECTED,
        STATE_DISCONNING,
    };

    int state;
    int sock;

    struct sockaddr_in self;
    struct sockaddr_in peer;

    u32 sessid;     //connect session id
    u32 send_tid;   //send transaction id
    u32 recv_tid;   //recv transaction id
	std::list<string> send_list;
	std::list<string> recv_list;

    Thread thread;
    Mutex mutex;

    void clear();
    int send_str(const char *str, int len);
	int recv_str(char *str, int len, bool accept);
    int send_conn_cfm(u32 sessid);
    int send_disc(u32 sessid);
    int send_disc_cfm(u32 sessid);
    int send_pack(u32 sessid, u32 tid, u32 seq, const string &pack);
	int send_pack_cfm(u32 sessid, u32 tid, u32 seq);
	static void entry(void * arg);
	int process();
};

}
#endif //_SERVER_H_
